package org.ohos.rajawali3d.examples.examples.general;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.opengl.Bitmap;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.materials.textures.ATexture;
import org.rajawali3d.math.vector.Vector3;

public class SkyboxFragment extends AExampleFragment {

    private static final String TAG = "SkyboxFragment";

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        DirectionalLayout ll = new DirectionalLayout(getContext());
        ComponentContainer.LayoutConfig lc = ll.getLayoutConfig();
        lc.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        lc.height = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        ll.setOrientation(DirectionalLayout.VERTICAL);
        ll.setAlignment(LayoutAlignment.HORIZONTAL_CENTER | LayoutAlignment.TOP);

        Button label = new Button(getContext());
        label.setText("Toggle Skybox");
        label.setTextSize(50);
        label.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        label.setHeight(80);
        label.setTextColor(Color.BLACK);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFADADAD));
        label.setBackground(shapeElement);
        label.setClickable(true);
        label.setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
        label.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                System.out.println("Renderer clicked");
                ((SkyboxRenderer) mRenderer).onButtonClick(component);
            }
        });
        ll.addComponent(label);
        mLayout.addComponent(ll);
        mLayout.moveChildToFront(ll);
        //ll.setClickedListener((SkyboxRenderer) mRenderer);
        mRenderSurface.setTransparent(false);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);

        return mLayout;
    }

//  protected void onBeforeApplyRenderer() {
//    mRenderSurface.setFrameRate(8);
//    mRenderSurface.setRenderMode(GLSurfaceProvider.RENDERMODE_WHEN_DIRTY);
//  }

    @Override
    public AExampleRenderer createRenderer() {
        mRenderer = new SkyboxRenderer(getContext(), this);
        return ((SkyboxRenderer) mRenderer);
    }

    public static final class SkyboxRenderer extends AExampleRenderer {

        boolean odd = true;

        public SkyboxRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            getCurrentCamera().setFarPlane(1000);
            /*
             * Skybox images by Emil Persson, aka Humus. http://www.humus.name humus@comhem.se
             */
            try {
                PixelMap[] maps = new PixelMap[6];
                maps[0] = Bitmap.getPixelMapFromResource(ResourceTable.Media_posx, mContext);
                maps[1] = Bitmap.getPixelMapFromResource(ResourceTable.Media_negx, mContext);
                maps[2] = Bitmap.getPixelMapFromResource(ResourceTable.Media_posy, mContext);
                maps[3] = Bitmap.getPixelMapFromResource(ResourceTable.Media_negy, mContext);
                maps[4] = Bitmap.getPixelMapFromResource(ResourceTable.Media_posz, mContext);
                maps[5] = Bitmap.getPixelMapFromResource(ResourceTable.Media_negz, mContext);
                getCurrentScene().setSkybox(maps);
//                getCurrentScene().setSkybox(ResourceTable.Media_posx, ResourceTable.Media_negx,
//                        ResourceTable.Media_posy, ResourceTable.Media_negy, ResourceTable.Media_posz, ResourceTable.Media_negz);
            } catch (ATexture.TextureException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onRender(long elapsedRealtime, double deltaTime) {
            super.onRender(elapsedRealtime, deltaTime);
            getCurrentCamera().rotate(Vector3.Axis.Y, -0.2);
        }

        public void onButtonClick(Component v) {
            try {
                if (odd) {
                    /*
                     * Skybox images by Emil Persson, aka Humus. http://www.humus.name humus@comhem.se
                     */
                    getCurrentScene().updateSkybox(ResourceTable.Media_posx2, ResourceTable.Media_negx2,
                            ResourceTable.Media_posy2, ResourceTable.Media_negy2, ResourceTable.Media_posz2, ResourceTable.Media_negz2);
                } else {
                    /*
                     * Skybox images by Emil Persson, aka Humus. http://www.humus.name humus@comhem.se
                     */
                    getCurrentScene().updateSkybox(ResourceTable.Media_posx, ResourceTable.Media_negx,
                            ResourceTable.Media_posy, ResourceTable.Media_negy, ResourceTable.Media_posz, ResourceTable.Media_negz);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                odd = !odd;
            }
        }
    }

}
